package org.example.controller;


import org.example.domain.Fruit;
import org.example.response.ApiResponse;
import org.example.service.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/api/upload")
public class UploadController {

    @Autowired
    private UploadService uploadService;

    /***
     * 文件上传
     * @param imageFile
     * @return
     */
    @PostMapping
    public ApiResponse<String> upload(@RequestPart(value = "file", required = true) MultipartFile imageFile) {
        try {
            String fileId = uploadService.upload(imageFile);
            return ApiResponse.success(fileId);
        } catch (Exception e) {
            return ApiResponse.error(500, "文件上传失败: " + e.getMessage());
        }
    }

}
